import os
import gi
from config import path
from config import lang_os
from config import read_json
from config import write_json
import c_code
import pars

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf


class Handler:
    def __init__(self) -> None:
        self.settings()

        self.builder = Gtk.Builder()
        if lang_os == "ru_RU":
            self.builder.add_from_file(f"{path}/ui/Несохранённый_ru.glade")
        else:
            self.builder.add_from_file(f"{path}/ui/Несохранённый_en.glade")
        self.builder.connect_signals(self)
        self.window = self.builder.get_object("window1")
        self.icon_equipment = self.builder.get_object("icon_equipment")
        self.icon_personal = self.builder.get_object("icon_personal")
        self.icon_system = self.builder.get_object("icon_system")
        self.icon_others = self.builder.get_object("icon_others")
        self.func_icon(self.icon_equipment, "Оборудование")
        self.func_icon(self.icon_personal, "Личные")
        self.func_icon(self.icon_system, "Система")
        self.func_icon(self.icon_others, "Прочее")

    def func_icon(self, icon_equipment, key):
        list_icon = Gtk.ListStore(GdkPixbuf.Pixbuf, str)
        icon_equipment.set_model(list_icon)
        icon_equipment.set_pixbuf_column(0)
        icon_equipment.set_text_column(1)
        for data in self.config[key]:
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(data["icon"], 32, 32, True)
            list_icon.append([pixbuf, data["text"]])
        icon_equipment.connect(
            "button-press-event", self.on_mouse_click, icon_equipment, key
        )

    def settings(self):
        self.link_doc_app = ""
        self.config = read_json(f"{path}/locale/config.json")

    def show(self):
        self.window.show_all()
        Gtk.main()

    def on_mouse_click(self, widget, event, icon_equipment, key):
        if event.button == 1:
            data = widget.get_path_at_pos(int(event.x), int(event.y))
            list_index = list(icon_equipment.get_selected_items())
            if list_index:
                index = list_index[0][0]

                if data:
                    if event.type == Gdk.EventType.BUTTON_PRESS:
                        self.link_doc_app = self.config[key][index]["link"]
                        # self.cheek_button_doc()
                    if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS:
                        call = self.config[key][index]["call_app"]
                        call = f"{call} > /dev/null 2>&1 &"
                        # call = call.encode("utf-8")
                        # c_code.obj_c_tools.execute(call)
                        os.system(call)

    def on_button_doc_clicked(self, button):
        self.func_aboutdialog()

    def on_button_exit_clicked(self, button):
        Gtk.main_quit()

    def cheek_button_doc(self):
        self.config["doc"] = True
        write_json(f"{path}/locale/config.json", self.config)
        self.button_doc()

    def on_doc_button_exit_clicked(self, button):
        self.func_aboutdialog()

    def func_aboutdialog(self):
        if self.config["doc"]:
            self.button_doc()
        else:
            obj_dialog = Dialog()
            obj_dialog.set_link_doc(self.link_doc_app)
            d = {
                "button_doc": self.button_doc,
                "cheek_button_doc": self.cheek_button_doc,
            }
            obj_dialog.set_func_id(d)
            obj_dialog.show()

    def button_doc(self):
        link = f"xdg-open {self.link_doc_app}"
        # link = link.encode("utf-8")
        #
        # c_code.obj_c_tools.execute(link)
        os.system(link)
        self.link_doc_app = ""


class Dialog:
    def __init__(self) -> None:
        self.builder = Gtk.Builder()
        if lang_os == "ru_RU":
            self.builder.add_from_file(f"{path}/ui/Dialog_ru.glade")
        else:
            self.builder.add_from_file(f"{path}/ui/Dialog_en.glade")
        self.builder.connect_signals(self)
        self.window = self.builder.get_object("windows2")
        self.link_doc = ""
        self.dict_func_id = {}

    def show(self):
        self.window.run()
        Gtk.main()

    def set_link_doc(self, link_doc):
        self.link_doc = link_doc

    def set_func_id(self, dict_func_id):
        self.dict_func_id = dict_func_id

    def on_button_reference_online_clicked(self, button):
        self.dict_func_id["button_doc"]()

    def on_doc_button_exit_clicked(self, button):
        self.window.destroy()

    def on_doc_cheek_button_toggled(self, button):
        self.dict_func_id["cheek_button_doc"]()


Handler().show()
